package demo6;

import java.util.Comparator;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/4/26 22:47
 */
public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("lixiang",20);
        Student student2 = new Student("wangba",100);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));
        // System.out.println(student2 > student1);
        //System.out.println(student1.compareTo(student2));
    }

    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a < b);
    }
}
class AgeComparator implements Comparator<Student>{//表示比较的是Student类

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age- o2.age;
    }
}
class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.age > o.age){
            return 1;
        }else if (this.age == o.age){
            return 0;
        }else {
            return -1;
        }
    }
}